define(['js/lib/codemirror/lib/codemirror',
    'js/lib/codemirror/addon/hint/show-hint',
    'js/lib/codemirror/addon/hint/elite-hint',
    'js/lib/codemirror/mode/javascript/javascript',
    'js/lib/codemirror/mode/properties/properties'], function(CodeMirror) {
	return {
		cpTest: function(runtime, box) {
			Helper.loadCSS($E.url + '/js/lib/codemirror/lib/codemirror.css', 'codemirror');
			Helper.loadCSS($E.url + '/js/lib/codemirror/addon/hint/show-hint.css', 'codemirror-sh');
			Helper.loadCSS($E.url + '/js/lib/codemirror/theme/elite-ve.css', 'codemirror-elite');
			
			var container = $('<div class="wrapper" style="display:flex;flex-flow:column"><div style="padding:5px;border-bottom:1px solid #ccc;"><button type="button" class="cmdDebug">临时命令调试</button></div></div>');
			box.append(container);
			$('.cmdDebug', container).on('click', function() {
				var dom = $('<div style="position:relative;width:500px;height:300px"><textarea></textarea></div>');
				$F.createDialog(dom, {
					contentCSS: {
						padding: 0
					},
					removable: true,
					title: '运行临时命令',
					onOpen: function() {
						jsedit = CodeMirror.fromTextArea(dom.find('textarea')[0], {
							mode : "text/json",
							lineNumbers : true,
							indentUnit: 4,
							tabSize: 4
						});
					    $(jsedit.display.wrapper).addClass('elite-ve elite-ve-focus');
					    jsedit.setValue(window.sessionStorage.getItem('cp-cmd') || '');
					},
					onSubmit: function(close) {
						var cmd = jsedit.getValue();
						window.sessionStorage.setItem('cp-cmd', cmd);
						close();
						try {
							runtime.instance._doCmd('cpCmd', {
								name: 'test',
								items: {
									type: 900,
									content: cmd
								}
							}, {}, runtime, 'cpTest')
						} catch(e) {
							console.error(e)
						}
					},
					onCancel: function(close) {
						close();
					}
				})
			});
			
			var cmdTree = $('<div class="cmdTree" style="flex:1;height:100%" />').appendTo(container), jsedit;
			cmdTree = cmdTree.wisTree({
				completely: true,
				expandAll: true,
				lock: true,
				awesome: {
					leaf: 'code'
				},
				labels: function(e) {
					if (e.folder)
						return e.name;
					else
						return {dom: e.id + '<span class="gray"> - ' + e.name + '</span>', 'tip': e.name};
				},
				extendBtns: [{
					icon: 'fas fa-play-circle',
					title: 'run',
					filter: function(node) {
						return !node.folder;
					},
					callback: function(node) {
//						console.log(node);
						var dom = $('<div style="position:relative;width:500px;height:300px"><textarea></textarea></div>');
						$F.createDialog(dom, {
							contentCSS: {
								padding: 0
							},
							removable: true,
							title: '组件命令运行入參',
							onOpen: function() {
								jsedit = CodeMirror.fromTextArea(dom.find('textarea')[0], {
									mode : "text/json",
									lineNumbers : true,
									indentUnit: 4,
									tabSize: 4
								});
							    $(jsedit.display.wrapper).addClass('elite-ve elite-ve-focus');
							    jsedit.setValue(window.sessionStorage.getItem('cp-test') || '');
							},
							onSubmit: function(close) {
								var R = jsedit.getValue();
								window.sessionStorage.setItem('cp-test', R);
								close();
								try {
									R = eval('let x = function(){return ' + R + '}; x();');
									console.log('PARAMS: ', R)
									runtime.instance.doCmd(node.id, R, runtime, 'cpTest', node.internal);
								} catch(e) {
									console.error(e)
								}
							},
							onCancel: function(close) {
								close();
							}
						})
					}
				}]
			});
			// 动态页面命令编辑
			var folders = [], map = {}, folder, parentNode, cmds = runtime.struct.cmds;
			var internal = {
				id: 'internal',
				name: '内部命令',
				folder: true,
				children: []
			}
			for(var id in cmds.internal) {
				internal.children.push({
					id: id,
					name: cmds.internal[id].name,
					items: cmds.internal[id].items,
					pId: internal.id,
					internal: true
				})
			}
			folders.push(internal);
			for(var id in cmds.folder) {
				map[id] = {
					id: id,
					name: (cmds.folder[id].name || '未命名'),
					pId: cmds.folder[id].pId,
					folder: true,
					children: []
				}
			}
			for(var id in map) {
				folder = map[id];
				if (folder.pId) {
					parentNode = map[folder.pId];
					if (parentNode)
						parentNode.children.push(folder);
					else
						folder.push(folder);
				} else
					folders.push(folder)
			}
			
			var cmd, item;
			for(var id in cmds.extend) {
				cmd = cmds.extend[id];
				item = {
					id: cmd.id,
					name: cmd.name,
					items: cmd.items,
					folder: false
				}
				if (!cmd.fid || !map[cmd.fid])
					folders.push(item);
				else
					map[cmd.fid].children.push(item);
			}
			cmdTree.setData(folders);
		}
	};
});
